﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Tour.Linq.TourDBML;
/// <summary>
///TourStaticData 的摘要说明
/// </summary>
public static class TourStaticData
{

    public static string UploadPicPath = System.Configuration.ConfigurationManager.AppSettings["UploadPicPath"];

    private static MyTourDataContext tdc = new MyTourDataContext();

    private static List<TourType> _TourTypeList;

    public static List<TourType> TourTypeList
    {
        get
        {
            if (_TourTypeList == null)
            {
                _TourTypeList = (from t in tdc.TourType select t).ToList();

            }
            return _TourTypeList;
        }
    }

    private static List<TourType> _TreeTourType;

    public static List<TourType> TreeTourType
    {
        get
        {
            if (_TreeTourType == null)
            {
                _TreeTourType = new List<TourType>();
                var parentType = from p in tdc.TourType
                                 where p.ParentID == 0
                                 select p;
                foreach (var parent in parentType)
                {
                    _TreeTourType.Add(parent);
                    _TreeTourType.AddRange(GetChildType(parent, 1));
                }
            }
            return _TreeTourType;
        }
    }

    private static List<TourArea> _TreeTourArea;

    public static List<TourArea> TreeTourArea
    {
        get
        {
            if (_TreeTourArea == null)
            {
                object o = new object();
                lock (o)
                {
                    if (_TreeTourArea == null)
                    {
                        _TreeTourArea = new List<TourArea>();
                        var parentType = tdc.TourArea.Where(p => p.ParentID == 0);
                        foreach (var parent in parentType)
                        {

                            _TreeTourArea.Add(parent);
                            _TreeTourArea.AddRange(GetChildType(parent, 1));
                        }

                        //_TreeTourArea = tempList;
                    }
                }
            }
            return _TreeTourArea;
        }
    }
    private static List<TourArea> GetChildType(TourArea t, int depet)
    {
        List<TourArea> childTree = new List<TourArea>();
        var childType = from c in tdc.TourArea
                        where c.ParentID == t.AreaID
                        select c;
        foreach (var child in childType)
        {
            string prefix = string.Empty;
            for (int i = 0; i < depet; i++)
            {
                prefix += "　　";
            }
            child.AreaName = prefix + "+------" + child.AreaName;
            childTree.Add(child);
            childTree.AddRange(GetChildType(child, depet+1));
        }
        return childTree;
    }

    private static List<TourType> GetChildType(TourType t, int depet)
    {
        List<TourType> childTree = new List<TourType>();
        var childType = from c in tdc.TourType
                        where c.ParentID == t.ID
                        select c;
        foreach (var child in childType)
        {
            string prefix = string.Empty;
            for (int i = 0; i < depet; i++)
            {
                prefix += "　　";
            }
            child.TypeName = prefix + "+------" + child.TypeName;
            childTree.Add(child);
            childTree.AddRange(GetChildType(child, depet + 1));
        }
        return childTree;
    }

    public static void Clean()
    {
        _TreeTourType = null;
        _TreeTourArea = null;
    }
}
